﻿function txtValid_Value(txtObj)
{
	var strCon,strVal;
	var iLen,ICount,iPos,ILenX;
	strVal=returnValue(txtObj.value);
	iLen=strVal.length;
	if (iLen == 0) 
	{
		alert("De nghi nhap lai gia tri");
		txtObj.focus();
		return;
	}
	if (!isNumber(strVal)) {
		alert("Đề nghị nhập giá trị là số");
		txtObj.value="";
		txtObj.value="0";
		txtObj.focus();
		return;
	}
	txtObj.value="";
	txtObj.value=strVal;
}
function validate_Value(oSrc, args)
{
	args.IsValid = isNumber(args.Value);
}
function isNumber(inputVal) {
	oneDecimal = false
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
		continue
		}
		if (oneChar == "." && !oneDecimal) {
		oneDecimal = true
		continue
		}
		if (oneChar < "0" || oneChar > "9") {
		return false
		}
	}
	return true
}

function returnValue(strVal){
	var strCon;
	var iLen,iPos;
	strCon="";
	iLen=strVal.length;
	iPos=0;
	if (iLen>1){
		for(var iIndex=0;iIndex<iLen;iIndex++){
			var curChar;
			curChar=strVal.charAt(iIndex);
			if (curChar=="0"){
				iPos=iPos+1;
			}
			else{
				break;
			}
		}
	}
	while ((iLen-1)>iPos){
		strCon=strVal.charAt(iLen-1)+strCon;
		iLen=iLen-1;
	}
	strCon=strVal.charAt(iPos)+strCon;
	return strCon;
}
function validateDate( strValue, strDateFormat )
{
	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	//check to see if in correct format
	if(!objRegExp.test(strValue))
		return false; //doesn't match pattern, bad date
	else{
		var strSeparator = strValue.substring(2,3);
		if (strSeparator != "/") strSeparator  = "/";
		var arrayDate = strValue.split(strSeparator);
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31, '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31};
		
		var strDay;
		var strMonth;
		if (strDateFormat == 'ddmmyyyy')
		{
			strDay = arrayDate[0];
			strMonth = arrayDate[1];
		}
		else if (strDateFormat == 'mmddyyyy')
		{
			strDay = arrayDate[1];
			strMonth = arrayDate[0];
		}
		var strYear = arrayDate[2];
		var intDay = eval(strDay);
		var intMonth = eval(strMonth);
		var intYear = eval(strYear);
		
		if ((intMonth > 12) || (intMonth <= 0))
		{
			return false;
		}
		else
		{
			//check if month value and day value agree
			if(arrayLookup[strMonth] != null) {
				if(intDay <= arrayLookup[strMonth] && intDay != 0)
				{
					return true; //found in lookup table, good date
				}
			}
			
			//check for February
			if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
				return true; //Feb. had valid number of days
		}
	}
	return false; //any other values, bad date8
}

function validateFromDate_vi_VN(oSrc, args)
{
	args.IsValid = validateDate(args.Value, 'ddmmyyyy');
}

function validateFromDate_en_US(oSrc, args)
{
	args.IsValid = validateDate(args.Value, 'mmddyyyy');
}

//Ham set Forcus vao mot doi tuong	
	function focusObject(obj)
	{		          
        if(document.getElementById(obj).disabled) return;
		
		var type = document.getElementById(obj).type;
		if(type == 'text'||type == 'textarea')
		{
			document.getElementById(obj).select();
		}
		document.getElementById(obj).focus();				
	}
	
/*	
* Thiết lập button mặc định (được click khi nhấn enter)
*/
function clickButton(e, buttonid)
{
	debugger;
	var bt = document.getElementById(buttonid); 
	if (typeof bt == 'object')
	{
		if(navigator.appName.indexOf("Netscape")>(-1))
		{ 
			if (e.keyCode == 13)
			{ 
					bt.click(); 
					return false; 
			} 
		} 
		if (navigator.appName.indexOf("Microsoft Internet Explorer")>(-1))
		{ 
			if (event.keyCode == 13)
			{ 
					bt.click(); 
					return false;
			} 
		}
	}
}

//Hàm thực hiện việc check hoặc uncheck các checkbox trong datagrid
function check_uncheck(Val)
{
  var ValChecked = Val.checked;
  var ValId = Val.id;
  var frm = document.forms[0];
  // Loop through all elements
  for (i = 0; i < frm.length; i++)
  {
// Look for Header Template's Checkbox
    
    //As we have not other control other than checkbox we just check following statement
    if (this != null)
    {
      if (ValId.indexOf('CheckAll') !=  - 1)
      {
// Check if main checkbox is checked,
        
        // then select or deselect datagrid checkboxes
        if (ValChecked)
          frm.elements[i].checked = true;
        else
          frm.elements[i].checked = false;
      }
    else if (ValId.indexOf('deleteRec') !=  - 1)
      
      {
        // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
        if (frm.elements[i].checked == false)
          frm.elements[1].checked = false;
      }
    } // if
  } // for
} // function

//Hàm thực hiện việc trả về xem người dùng có đồng ý xóa không?
function confirmMsg(frm)
{
// loop through all elements
  
  for (i = 0; i < frm.length; i++)
  {
    // Look for our checkboxes only
    if (frm.elements[i].name.indexOf("deleteRec") !=  - 1)
    {
      // If any are checked then confirm alert, otherwise nothing happens
      if (frm.elements[i].checked)
        return confirm('Bạn có Đồng ý xóa những dữ liệu đã chọn ?')
      
    }
  }
}

function HighlightRow(chkB)    {
    var oItem = chkB.children;
    xState=oItem.item(0).checked;    
    if(xState)
        {chkB.parentElement.parentElement.style.backgroundColor='lightcoral';
           // grdEmployees.SelectedItemStyle.BackColor
         chkB.parentElement.parentElement.style.color='white'; 
           // grdEmployees.SelectedItemStyle.ForeColor
        }else 
        {chkB.parentElement.parentElement.style.backgroundColor='white'; 
             //grdEmployees.ItemStyle.BackColor
         chkB.parentElement.parentElement.style.color='black'; 
             //grdEmployees.ItemStyle.ForeColor
        }
}
